package blue.blog.core.dao;

import org.springframework.stereotype.Repository;

import com.blue.jdbc.BaseDao;

import blue.blog.core.model.CmsDoc;

@Repository
public class CmsDocDao extends BaseDao<CmsDoc>
{
	public CmsDocDao()
	{
	}

	@Override
	protected void query(StringBuilder sql, CmsDoc param)
	{
		if (param != null)
		{
			if (param.getChannelId() != null && param.getChannelId() != -1)
				sql.append(" and a.channel_id=:channelId");
			
			if (param.getState() != null && param.getState() != -1)
				sql.append(" and a.state=:state");
			
			if (param.getTitle() != null && !param.getTitle().isEmpty())
			{
				sql.append(" and a.title like :title");
				param.setTitle("%" + param.getTitle() + "%");
			}
			
		}
		sql.append(" order by a.id desc");
	}

	public boolean exist(String url)
	{
		String sql = "select count(*) from cms_doc where url=?";
		int n = jdbcTemplate.queryForObject(sql, Integer.class, url);
		return n > 0;
	}
	
}
